import { _decorator, Component, Node, tween, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

import PromiseMgr from './PromiseMgr';
import { FrontCardBase } from './FrontCardBase';
import { DEV } from 'cc/env';

@ccclass('CardCase')
export class CardCase extends Component {

    @property({displayName: DEV && 'containerNode节点', type: Node})
    protected containerNode: Node | null = null;

    @property({displayName: DEV && 'cardNode节点', type: Node})
    protected cardNode: Node | null = null;

    protected card: FrontCardBase = null;
    protected cardArr: FrontCardBase[] = null;
    protected getFrontArrCard(index: number) {
        return this.containerNode.children[index];
    }
    protected get frontArrayCard() {
        return this.containerNode.children[this.containerNode.children.length - 1];
        // return this.containerNode.children[2];
    }
    protected onLoad() {
        this.init();
    }

    /**
     * 初始化
     */
    protected init() {
        this.card = this.cardNode.getComponent(FrontCardBase);
        this.play();
        
        // this.cardSendAnime();
    }

    public async cardSendAnime() {
        // 旋转两圈
        // await this.rotate(1);

        for (let index = 0; index < this.containerNode.children.length; index++) {
            console.log(index)
            
            // this.cardArr[] = this.getFrontArrCard(index);
            const frontCard = this.cardNode.getComponent(FrontCardBase);
            console.log(frontCard)
            
            await PromiseMgr.sleep(0.2); // 等一会
            
            frontCard.show(); // 替换卡片
            
            await frontCard.flipToFront();// 翻卡
           
            await PromiseMgr.sleep(2); // 等一会
        }
        return

        for (let index = 0; index < this.cardArr.length; index++) {
            console.log(index)
            const frontCard = this.cardNode.getComponent(FrontCardBase);
            
            await PromiseMgr.sleep(0.2); // 等一会
            
            frontCard.show(); // 替换卡片
            
            await frontCard.flipToFront();// 翻卡
           
            await PromiseMgr.sleep(2); // 等一会
            
        }
    }

    public async play() {
        const frontCard = this.card;
        
        // await this.rotate(1);// 旋转两圈
       
        await PromiseMgr.sleep(0.2); // 等一会
        
        frontCard.show();
        
        await frontCard.flipToFront();
        
        await PromiseMgr.sleep(2);// 等一会
        
        await frontCard.flipToBack(); // 翻卡
        
        await PromiseMgr.sleep(0.2);// 等一会
       
        this.play(); // 继续
    }

    /**
     * 旋转
     * @param round 圈数
     */
    public rotate(round: number) {
        return new Promise<void>(res => {
          const node = this.containerNode,
          time = 1 * round,
          { x, z } = this.node.eulerAngles,
          eulerAngles = new Vec3(x, 360 * round, z);
          tween(node)
            .by(time, { eulerAngles }, { easing: 'quadOut' })
            .call(res)
            .start();
        });
    }
}


